<template>
  <div class="app-container">
    <!--    <el-form :model="quemacParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">-->
    <!--      <el-form-item label="拨出号码" prop="sendTel">-->
    <!--        <el-input-->
    <!--          v-model="quemacParams.sendTel"-->
    <!--          placeholder="请输入拨出号码"-->
    <!--          clearable-->
    <!--          size="small"-->
    <!--          @keyup.enter.native="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="接听号码" prop="receiveTel">-->
    <!--        <el-input-->
    <!--          v-model="quemacParams.receiveTel"-->
    <!--          placeholder="请输入接听号码"-->
    <!--          clearable-->
    <!--          size="small"-->
    <!--          @keyup.enter.native="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="开始时间" prop="startAt">-->
    <!--        <el-input-->
    <!--          v-model="quemacParams.startAt"-->
    <!--          placeholder="请输入开始时间"-->
    <!--          clearable-->
    <!--          size="small"-->
    <!--          @keyup.enter.native="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="结束时间" prop="endAt">-->
    <!--        <el-input-->
    <!--          v-model="quemacParams.endAt"-->
    <!--          placeholder="请输入结束时间"-->
    <!--          clearable-->
    <!--          size="small"-->
    <!--          @keyup.enter.native="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
    <!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
    <!--      </el-form-item>-->
    <!--    </el-form>-->

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['bill:tel:add']"-->
    <!--        >新增-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['bill:tel:edit']"-->
    <!--        >修改-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['bill:tel:remove']"-->
    <!--        >删除-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['bill:tel:export']"-->
    <!--        >导出-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <!--    <el-table v-loading="loading" :data="telList" @selection-change="handleSelectionChange">-->
    <!--      <el-table-column type="selection" width="55" align="center"/>-->
    <!--      <el-table-column label="编号" align="center" prop="id"/>-->
    <!--      <el-table-column label="拨出号码" align="center" prop="sendTel"/>-->
    <!--      <el-table-column label="接听号码" align="center" prop="receiveTel"/>-->
    <!--      <el-table-column label="开始时间" align="center" prop="startAt"/>-->
    <!--      <el-table-column label="结束时间" align="center" prop="endAt"/>-->
    <!--      &lt;!&ndash;      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">&ndash;&gt;-->
    <!--      &lt;!&ndash;        <template slot-scope="scope">&ndash;&gt;-->
    <!--      &lt;!&ndash;          <el-button&ndash;&gt;-->
    <!--      &lt;!&ndash;            size="mini"&ndash;&gt;-->
    <!--      &lt;!&ndash;            type="text"&ndash;&gt;-->
    <!--      &lt;!&ndash;            icon="el-icon-edit"&ndash;&gt;-->
    <!--      &lt;!&ndash;            @click="handleUpdate(scope.row)"&ndash;&gt;-->
    <!--      &lt;!&ndash;            v-hasPermi="['bill:tel:edit']"&ndash;&gt;-->
    <!--      &lt;!&ndash;          >修改</el-button>&ndash;&gt;-->
    <!--      &lt;!&ndash;          <el-button&ndash;&gt;-->
    <!--      &lt;!&ndash;            size="mini"&ndash;&gt;-->
    <!--      &lt;!&ndash;            type="text"&ndash;&gt;-->
    <!--      &lt;!&ndash;            icon="el-icon-delete"&ndash;&gt;-->
    <!--      &lt;!&ndash;            @click="handleDelete(scope.row)"&ndash;&gt;-->
    <!--      &lt;!&ndash;            v-hasPermi="['bill:tel:remove']"&ndash;&gt;-->
    <!--      &lt;!&ndash;          >删除</el-button>&ndash;&gt;-->
    <!--      &lt;!&ndash;        </template>&ndash;&gt;-->
    <!--      &lt;!&ndash;      </el-table-column>&ndash;&gt;-->
    <!--    </el-table>-->

    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="quemacParams.pageNum"-->
    <!--      :limit.sync="quemacParams.pageSize"-->
    <!--      @pagination="getList"-->
    <!--    />-->

    <!-- 添加或修改话单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="拨出号码" prop="sendTel">
          <el-input v-model="form.sendTel" placeholder="请输入拨出号码"/>
        </el-form-item>
        <el-form-item label="接听号码" prop="receiveTel">
          <el-input v-model="form.receiveTel" placeholder="请输入接听号码"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-col :span="1.5">
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
        v-hasPermi="['bill:tel:export']"
      >导出
      </el-button>
    </el-col>
    <vue-xlsx-table style="margin: 0 20px;" @on-select-file="handleSelectedFile">
      <i style="width: 25px" class="el-icon-download"></i> 导入
    </vue-xlsx-table>

    <el-col :span="1.5">
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        @click="getAnyList"
      >执行分析
      </el-button>
    </el-col>
    <el-table
      :data="telList1"
      border
      @selection-change="handleSelectionChange"
      style="margin: 10px 0 0 0;"
    >
      <el-table-column type="selection" fixed align="center"></el-table-column>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="拨出号码" align="center" prop="sendTel"/>
      <el-table-column label="接听号码" align="center" prop="receiveTel"/>
      <el-table-column label="开始时间" align="center" prop="startAt"/>
      <el-table-column label="结束时间" align="center" prop="endAt"/>
    </el-table>

    <div>
      <!--      <el-form style="display: inline-block;margin: 20px" ref="form" :model="form" label-width="80px">
              <el-form-item label="异常号码" prop="sendTel">
                <el-select v-model="sendTel">
                  <el-option v-for="item in sendTelList" :value="item"/>
                </el-select>
              </el-form-item>
            </el-form>

            <el-button style="display: inline-block" type="primary" @click="getAnyList">搜 索</el-button>-->

      <div style="margin: 0 50px">
        <div id="echarts_box" style="min-width: 1100px;height:500px; display: inline-block"></div>
      </div>

      <div style="width: 100%">
        <p style="width: 1000px"></p>
      </div>

      <div style="width: 100%">
        <div v-if="isShow" style="width: 900px;height: 300px;margin-left: 150px;margin-bottom: 50px;">
          <p style="color: #00afff;font-weight: bold;">
            根据数据分析，异常号码为：{{ this.sendTel }} 的主机最近最频繁的通话次数为：
          </p>
          <p style="color: red;font-weight: bold;">
            {{ this.allList }}
          </p>
          <p style="color: #ce8a0c;font-weight: bold;">
            该主机号码行为较为反常，可持续追踪
          </p>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import {listTel, getTel, delTel, addTel, updateTel, getTypeList} from "@/api/bill/tel";
import echarts from "echarts";

export default {
  name: "Tel",
  data() {
    return {
      sendTel: null,
      form: {
        mobile: '',
      },
      isShow: false,
      sendTelList: [],
      allList: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 话单表格数据
      telList: [],
      telList1: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      quemacParams: {
        pageNum: 1,
        pageSize: 10,
        sendTel: null,
        receiveTel: null,
        startAt: null,
        endAt: null
      },
      // 表单参数
      // 表单校验
      rules: {
        sendTel: [
          {required: true, message: "拨出号码不能为空", trigger: "blur"}
        ],
        receiveTel: [
          {required: true, message: "接听号码不能为空", trigger: "blur"}
        ],
        startAt: [
          {required: true, message: "开始时间不能为空", trigger: "blur"}
        ],
        endAt: [
          {required: true, message: "结束时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    // this.getList();
  },
  methods: {

    getAnyList() {
      this.telList = this.telList1
      var array = []
      for (let i = 0; i < this.telList.length; i++) {
        let demo = {
          'id': this.telList[i].id,
          'senTel': this.telList[i].senTel,
          'receiveTel': this.telList[i].receiveTel,
          'createAt': this.telList[i].createAt,
          'endAt': this.telList[i].endAt
        }
        console.log('demo==>',demo)

        array.push(demo)
      }
      console.log('this.array==>', this.array)
      var chartDom = document.getElementById('echarts_box');
      var myChart = echarts.init(chartDom);
      getTypeList(this.telList1).then(response => {

        console.log("response==>", response)
        // const data = [["2000-06-05", 116], ["2000-06-06", 129], ["2000-06-07", 135], ["2000-06-08", 86], ["2000-06-09", 73], ["2000-06-10", 85], ["2000-06-11", 73], ["2000-06-12", 68], ["2000-06-13", 92], ["2000-06-14", 130], ["2000-06-15", 245], ["2000-06-16", 139], ["2000-06-17", 115], ["2000-06-18", 111], ["2000-06-19", 309], ["2000-06-20", 206], ["2000-06-21", 137], ["2000-06-22", 128], ["2000-06-23", 85], ["2000-06-24", 94], ["2000-06-25", 71], ["2000-06-26", 106], ["2000-06-27", 84], ["2000-06-28", 93], ["2000-06-29", 85], ["2000-06-30", 73], ["2000-07-01", 83], ["2000-07-02", 125], ["2000-07-03", 107], ["2000-07-04", 82], ["2000-07-05", 44], ["2000-07-06", 72], ["2000-07-07", 106], ["2000-07-08", 107], ["2000-07-09", 66], ["2000-07-10", 91], ["2000-07-11", 92], ["2000-07-12", 113], ["2000-07-13", 107], ["2000-07-14", 131], ["2000-07-15", 111], ["2000-07-16", 64], ["2000-07-17", 69], ["2000-07-18", 88], ["2000-07-19", 77], ["2000-07-20", 83], ["2000-07-21", 111], ["2000-07-22", 57], ["2000-07-23", 55], ["2000-07-24", 60]];
        const data = response.data;
        // const data = [["2023-03-10", 200], ["2023-04-10", 400]];
        console.log('data==>', data)
        const dateList = data.map(function (item) {
          return item[0];
        });
        console.log('dateList==>', dateList)
        const valueList = data.map(function (item) {
          return item[1];
        });
        console.log('valueList==>', valueList)

        myChart.setOption({
          visualMap: [
            {
              show: false,
              type: 'continuous',
              seriesIndex: 0,
              min: 0,
              max: 400
            },
            {
              show: false,
              type: 'continuous',
              seriesIndex: 1,
              dimension: 0,
              min: 0,
              max: dateList.length - 1
            }
          ],
          title: [
            {
              left: 'center',
              text: '通话频率'
            },
            {
              top: '55%',
              left: 'center',
              text: '通话频率'
            }
          ],
          tooltip: {
            trigger: 'axis'
          },
          xAxis: [
            {
              data: dateList
            },
            {
              data: dateList,
              gridIndex: 1
            }
          ],
          yAxis: [
            {},
            {
              gridIndex: 1
            }
          ],
          grid: [
            {
              bottom: '60%'
            },
            {
              top: '60%'
            }
          ],
          series: [
            {
              type: 'line',
              showSymbol: false,
              data: valueList
            },
            {
              type: 'line',
              showSymbol: false,
              data: valueList,
              xAxisIndex: 1,
              yAxisIndex: 1
            }
          ]
        });
        this.allList = response.data;
        if (this.allList != null) {
          this.isShow = true;
        }
      });
    },

    // Excel导入
    handleSelectedFile(convertedData) {
      this.telList1 = []
      console.log('convertedData==>', convertedData)
      let {header: tableHeader, body: tableData} = convertedData;
      const arr = convertedData.body.map((item, index) => {
        // let list1 = this.options6.filter(function(item1) {
        //   if (item1.dictLabel == item.版次) {
        //     return item1;
        //   }
        // });
        return {
          id: item.编号,
          sendTel: item.拨出号码,
          receiveTel: item.接听号码,
          startAt: item.开始时间,
          endAt: item.结束时间,
        };
      });
      this.telList1 = this.telList1.concat(arr);
      // this.formData.bomList = this.formData.bomList.concat(arr);
    },


    /** 查询话单列表 */
    getList() {
      this.loading = false;
      listTel(this.quemacParams).then(response => {
        this.telList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sendTel: null,
        receiveTel: null,
        startAt: null,
        endAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.quemacParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加话单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改话单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除话单编号为"' + ids + '"的数据项？').then(function () {
        return delTel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport(row) {
      const ids = row.id || this.ids;
      this.download('bill/tel/export?ids=' + ids, {
        ...this.quemacParams
      }, `tel_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
